package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.xml.transform.Result;

public class DatabaseConnection 
{	
	
	private static String URL = "jdbc:mysql://localhost/atm";
	private static String USERNAME = "root";
	private static String PASSWORD = "";
    private static String DRIVER = "com.mysql.jdbc.Driver";
	
	private Connection connection = null;
	private PreparedStatement preparedStatement = null; 
	private ResultSet resultSet = null;
	
    public static void setDatabaseInfo(String sqlUrl, String sqlPort, String sqlUsername, String sqlPassword, String db)
    {
    	URL ="jdbc:mysql://" + sqlUrl + ":" + sqlPort + "/";
    	USERNAME = sqlUsername;
    	PASSWORD = sqlPassword;

    	if(db != null && db.length() > 0)
    		URL += db;
    }
	
	
	public DatabaseConnection()	
	{
		try 
		{
			connection = 
					DriverManager.getConnection( URL, USERNAME, PASSWORD );
		}	// end try
		catch ( Exception sqlException )
		{
			sqlException.printStackTrace();
		}	// end catch
	}	// end DatabaseConnection constructor

	public boolean checkUser(int username, int password)
	{
		String sql = "SELECT * FROM customers WHERE user_id = "+username+" AND password = "+password+";";
		try {
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next())
				return true;
			else
				return false;
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
}	// end DatabaseConnection class
